Conversation
Summary of ChangesHello @oI0ck, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request introduces comprehensive support for POSIX standard semaphore interfaces within the Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This PR introduces POSIX semaphore implementation. The changes are extensive, adding new headers and implementation files for named and unnamed semaphores. The overall structure is good, but I've found several critical issues in the implementation, including race conditions, incorrect logic leading to deadlocks, and potential memory corruption. There are also some areas where the code can be simplified and made more robust. Please address the critical issues before merging.
posix/sem.c
Outdated
| memset(path, 0, sizeof(path)); | ||
| strcpy(path, SEMAPHORE_PATH); | ||
| strcpy(path + strlen(SEMAPHORE_PATH), dent->d_name); |
There was a problem hiding this comment.
hm yeah, why not snprintf?
There was a problem hiding this comment.
I think that it is not worth to have the overhead of parsing the format string when just cating two strings together (ironic to consider this overhead but allocate PATH_MAX on the stack XD).
Though, there is a bug in here because dent->d_name can be PATH_MAX and a path of such length will cause a OOB write here.
I'll change this concatenation to use strlcpy in the next revision.
ba94ea8 to
da0f0fd
Compare
Unit Test Results9 523 tests 8 931 ✅ 52m 28s ⏱️ Results for commit f3710e3. ♻️ This comment has been updated with latest results. |
da0f0fd to
b6d1928
Compare
b6d1928 to
20f142f
Compare
sys/semaphore.c
Outdated
| if (s != NULL) { | ||
| ret = mutexTry(s->mutex); | ||
| if (ret != EOK) { | ||
| return ret; | ||
| } | ||
| else if (s->v <= 0) { | ||
| ret = -EAGAIN; | ||
| } | ||
| else { | ||
| --s->v; | ||
| } | ||
| } | ||
| else { | ||
| ret = -EINVAL; | ||
| } | ||
|
|
||
| mutexUnlock(s->mutex); | ||
| return ret; |
There was a problem hiding this comment.
if s == NULL we unlock a mutex that wasn't locked. It should not be an issue, but it's not what should be happening. Seems like we could just return in L105 if s == NULL
sys/semaphore.c
Outdated
| else { | ||
| } |
There was a problem hiding this comment.
not needed. MISRA only needs it if there's an if ... else if ... block
sys/semaphore.c
Outdated
|
|
||
| ret = mutexTry(s->mutex); | ||
| if (ret != EOK) { | ||
| ret = 0; |
There was a problem hiding this comment.
returning here would be more readable imo
posix/sem.c
Outdated
| else { | ||
| } |
posix/sem.c
Outdated
| else { | ||
| } |
posix/sem.c
Outdated
| else { | ||
| } |
posix/sem.c
Outdated
| else { | ||
| err = _sem_open(sem); | ||
| } | ||
|
|
There was a problem hiding this comment.
nitpick: else not needed as if already returns.
posix/sem.c
Outdated
| } | ||
| else { | ||
| SET_ERRNO(-ENOMEM); | ||
| return SEM_FAILED; | ||
| } | ||
| } | ||
| else { | ||
| SET_ERRNO(-EINVAL); | ||
| return SEM_FAILED; | ||
| } | ||
| } |
There was a problem hiding this comment.
nitpick: this seems hard to read. consider handling resources etc. maybe this way:
/* acquire */
val = malloc(...);
do {
err = call1();
if (err < 0) {
break;
}
err = call2();
if (err < 0) {
break;
}
} while (0);
/* cleanup - called always on error */
if (err < 0) {
free(val);
}
return err;There was a problem hiding this comment.
Oh, yeah, I wrote the code around my wrong presumption that each if has to have a matching else at all times.
I'll clean all of this up.
include/semaphore.h
Outdated
|
|
||
| #define SEM_FAILED ((sem_t *)0xDAAB0000) | ||
|
|
||
| typedef struct _sem_t { |
There was a problem hiding this comment.
_sem_t identifier is not used anywhere. If it's not required by POSIX - remove it.
include/sys/semaphore.h
Outdated
| #define SEMAPHORE_PATH ("/dev/posix/sem/") | ||
| #define SEMCTL_PATH ("/dev/posix/semctl") |
There was a problem hiding this comment.
nitpick: we don't seem to use parenthesis to define string in macros as they interfere with string concatenation by the compiler (we couldn't, for example, write code like const char *path = SEMAPHORE_PATH "my_sem" in the client).
20f142f to
cf48092
Compare
JIRA: RTOS-1088
JIRA: RTOS-1088
It might be better to keep OS specific constants in corresponding sys/ headers JIRA: RTOS-1088
JIRA: RTOS-1088
cf48092 to
f3710e3
Compare
Description
This PR introduces implementation of POSIX standard semaphore interfaces.
Types of changes
How Has This Been Tested?
ia32-generic-qemuChecklist:
Special treatment